package com.rajaev.dtu.tractor.interfaces;

import com.rajaev.dtu.tractor.app.service.ProjectTeamService;
import com.rajaev.dtu.tractor.app.service.UserService;
import com.rajaev.dtu.tractor.domain.entity.ProjectTeamPo;
import com.rajaev.dtu.tractor.domain.entity.UserInfo;
import com.rajaev.dtu.tractor.infrastructure.helper.UserSecurityUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@RestController
public class ProjectTeamController  {

    @Autowired
    ProjectTeamService projectTeamService;

    @Autowired
    UserSecurityUtil userSecurityUtil;

    @Autowired
    UserService userService;


    @GetMapping("/projectTeam/queryAll")
    public List<ProjectTeamPo> queryAllProjectTeam(HttpServletRequest request) {
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();

        List<Integer> customerIds = userService.selectUserCustomerIds(userInfo.getId());
        if (CollectionUtils.isEmpty(customerIds)) {
            return Collections.emptyList();
        }
        List<ProjectTeamPo> list = projectTeamService.selectProjectTeam(customerIds);
        return list;
    }

    /**
     * 新增项目组
     *
     *
     */
    @PostMapping("/projectTeam/add")
    public void addProjectTeam(@RequestBody ProjectTeamPo projectTeamPo) {
        projectTeamService.insertProjectTeam(projectTeamPo);
    }


    @PostMapping("projectTeam/update")
    public void update(@RequestBody ProjectTeamPo projectTeamPo){
        projectTeamService.update(projectTeamPo);
    }

    /**
     * 删除项目组
     *
     * @param id 主键
     */
    @PostMapping("/projectTeam/delete")
    public void delete(@RequestParam(name = "id", required = true) Integer id) {
        projectTeamService.delete(id);
    }


}
